
import { specsList, specsCount } from '../../../request/api';
const state = {
    specslist: [],//商品属性列表
    size: 3,//每页展示的几条数据
    page: 1,//当前页码
    count: 6,//总条数

}
const mutations = {
    GET_SPECS(state, payload) {
        state.specslist = payload;
    },
    GET_COUNT(state, payload) {
        state.count = payload;
    },
    CHANGE_PAGE(state,payload){
        state.page = payload
    }
}
const actions = {
    // 获取属性列表
    getSpecsList(context) {
        specsList({
            size: context.state.size,
            page: context.state.page
        }).then(res => {
            if (res.code == 200) {
                // console.log(context.state.page);

                if (context.state.page != 1 && res.list==false) {
                    // console.log(context.state.page);
                    context.dispatch('changePage',context.state.page-1)
                }else{
                    context.commit('GET_SPECS', res.list)

                }
            }
        })
    },
    // 获取总条数
    getCount({ commit }) {
        specsCount().then(res => {
            // console.log(res);
            commit('GET_COUNT', res.list[0].total)
        })
    },
    // 更改页码 重新渲染页面
    changePage(context, payload) {
        // console.log(context,payload);
        context.commit('CHANGE_PAGE',payload)
        // 重新渲染
        context.dispatch('getSpecsList')
    }

}
const getters = {
    specs_list(state) {
        return state.specslist
    },
    specs_size(state) {
        return state.size
    },
    specs_count(state) {
        return state.count
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}